jQuery(function($) {
	var doc = $(document);
    var role = 0;
	//切换投资人和创业者
	doc.on("click", "button", function(event) {
		var target = event.target;
		if (target.tagName === "BUTTON") {
			if (target.className.indexOf("investor") > -1 || target.className.indexOf("entrepr") > -1) {
				var btns = $(".register_radio button"),
					investor = $(".investor");
                role = $(this).attr('data-type')
				btns.removeClass("selected");
				$(this).addClass("selected");
			}
		}
	});
	
	//发送验证码后设置60秒等待时间
    var Time = {
        timer: 0,
        seconds: 60
    };

    jQuery.ajaxSetup({
        case:false,
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
    });

	function setTimeOut() {
		var btn = $(".code_btn");
        btn.html(Time.seconds-- + '秒后重新发送');
        btn[0].disabled = "disabled";
        btn.eq(0).addClass("reColor");
        clearInterval(Time.timer);
        Time.timer = setInterval(function(){
            var seconds = Time.seconds--;
            if (seconds === 0) {
                btn.eq(0).removeClass("reColor");
                clearInterval(Time.timer);
                btn.html("再次发送验证码");
                btn[0].disabled = "";
            } else {
                btn.html(seconds + '秒后重新发送');
            }
        }, 1000);
	}
	
	doc.on("click", ".code_btn", function() {
		if (!done.phone) {
			errorMessage($(".mobile_"), "请输入手机号码！");
			return;
		}
        var phone = $('.moblie').val();
        $.ajax({
            type:"post",
            url:getVerifyCodeByPhoneUrl,
            dataType:"json",
            data:{"phone":phone},
            success:function(obj){
                if(obj.level == "success"){
                    Time.seconds = 60;
                    setTimeOut();
                }
            },
            error:function(obj){
                var datajson = obj.responseJSON;
                noty({type:datajson.level,text:datajson.message,timeout:600});
            }
        });
	});
	
	//错误信息
	function errorMessage(node, message) {
		node.show();
		node.html(message);
	}
	
	//验证注册信息
	var done = {		//提交表单时再次验证
		phone: false,
		paw: false,
		code: false
	};
	
	//校验手机号
	function checkMobile(elm) {
		var tel = /^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/g;

		if (!tel.test(elm.val())) {
			errorMessage($(".mobile_"), "请输入正确的手机号码！");
			elm.css({
				border: "1px solid #f3a3a3"
			});
			done.phone = false;
			return;
		}
		done.phone = true;
	}
	
	doc.on("blur", ".moblie", function() {
		 checkMobile($(this));
	});
	
	doc.on("focus", ".moblie", function() {
		$(".mobile_").hide();
	});
	
	//校验密码
	function checkPaw(elm) {
		var val = elm.val();
		if (!val) {
			errorMessage($(".password"), "密码不能为空！");
			elm.css({
				border: "1px solid #f3a3a3"
			});
			done.paw = false;	
			return;
		}
		done.paw = true;		
	}
	
	doc.on("blur", ".paw", function() {
		checkPaw($(this));
	});
	
	doc.on("focus", ".paw", function() {
		$(".password").hide();
	});
	
	//检测手机验证码
	function checkCode(elm) {
		var val = elm.val();
		if (!val) {
			errorMessage($(".code_"), "请输入短信验证码！");
			done.code = false;	
			return;
		}
		done.code = true;
	}
	
	doc.on("blur", ".code", function() {
		checkCode($(this));
	});
	
	doc.on("focus", ".code", function() {
		$(".code_").hide();
	});
	
	//提交注册
	doc.on("click", ".submitRegister", function() {
		checkMobile($(".moblie"));
		checkPaw($(".paw"));
		checkCode($(".code"));
		
		if (done.phone && done.paw && done.code) {
            doRegister();
		}
	});

    /** 回车键提交注册 */
    doc.keydown(function(event){
        if(event.keyCode==13){
            doRegister();
        }
    });

    /** Ajax提交注册信息 */
    function doRegister(){
        var phone = $('.moblie').val();
        var password = $('.paw').val();
        var code = $('.code').val();
        $.ajax({
            type:"post",
            url :SignUpUrl,
            dataType:"json",
            data:{"phone":phone,"password":password,"role":role,"verify_code_for_phone":code},
            success:function(obj){
                if(obj.level === "success"){
                    noty({text:obj.message,type:obj.level,
                        callback:{
                            afterClose:function(){
                                window.location.href=Home;
                            }
                        }
                    });
                }
            },
            error:function(obj){
                var datajson = obj.responseJSON;
                noty({text:datajson.message,type:datajson.level,timeout:600});
            }
        });
    }
});